<template>
  <div class="product-test">
    <div class="title">产品功能测试</div>
    <div v-loading="loading" class="result">
      <div v-show="testResultData.testResult === null" class="no-data">
        <img src="../../../../public/images/no-test-report.png" alt="">
        <div class="no-text">无测试报告</div>
      </div>
      <div v-show="testResultData.testResult !== null" class="box">
        <div>
          <span class="label">测试结论</span>
          <span v-if="testResultData.testResult === 0" class="result-data">
            <i class="iconfont icon-shangchuanchenggong"></i>
            通过
          </span>
          <span v-else class="result-data">
            <i class="iconfont icon-shibai"></i>
            不通过
          </span>
        </div>
        <div>
          <span class="label">测试报告名称</span>
          <span class="result-data">{{ testResultData.showName }}</span>
        </div>
        <div>
          <span class="label">上传时间</span>
          <span class="result-data">{{ testResultData.createTime }}</span>
        </div>
      </div>
    </div>
    <div class="btn">
      <el-button :disabled="rolesCode === 'SALE'" type="add" size="14" class="download" @click="downloadReport">
        <div class="box">
          <i class="iconfont icon-anniuxiazai"></i>
          <span class="text">下载报告</span>
        </div>
      </el-button>
      <el-button :disabled="rolesCode === 'SALE'" type="add" size="14" class="upload" @click="uploadReport">
        <div class="box">
          <i class="iconfont icon-anniushangchuan"></i>
          <span class="text">上传报告</span>
        </div>
      </el-button>
    </div>

    <el-dialog
      title="上传测试报告"
      :visible.sync="dialogVisible"
      width="587px"
      :before-close="handleClose"
      custom-class="dialog">
      <div class="list">
        <el-form ref="form" :model="form" label-width="0" class="form">
          <div>测试结果</div>
          <el-form-item>
            <el-radio-group v-model="form.testResult">
              <el-radio label="0">通过</el-radio>
              <el-radio label="1">不通过</el-radio>
            </el-radio-group>
          </el-form-item>
          <div>测试报告</div>
          <el-form-item>
            <el-upload
              class="upload-demo"
              action="/admin/sys-file/upload"
              :headers="headers"
              :before-upload="beforeUpload"
              :on-success="uploadSuccess"
              :on-error="uploadError"
              multiple
              :accept="allowedTypes">
              <el-button v-if="!uploadSuccessedFile" size="small" icon="el-icon-plus" class="upload-btn">点击上传</el-button>
              <el-input v-else v-model="file.name" class="file-name">{{ file.name }}</el-input>
            </el-upload>
            <div class="el-upload__tip">支持格式：pdf、xls、xlsx、doc、docx</div>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="close" size="14" @click="handleClose">取 消</el-button>
        <el-button
          type="primary"
          size="14"
          :disabled="form.testResult === '' || reportUrl === ''"
          @click="onsubmit">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getReport, addReport } from '@/api/product/product-details'
import { mapGetters } from 'vuex'
import store from '@/store'
export default {
  name: "productionTest",
  data() {
    return {
      headers: {
        'TENANT-ID': store.getters.userInfo.tenantId,
        Authorization: 'Bearer ' + store.getters.access_token
      },
      dialogVisible: false,
      form: {
        testResult: ''
      },
      file: {}, // 上传文件
      uploadSuccessedFile: false,
      allowedTypes: "pdf, xls, xlsx, doc, docx", // 允许上传的文件类型
      testResultData: {},
      reportUrl: '', // 上传成功后的url
      showName: '',
      loading: true
    }
  },
  computed: {
    ...mapGetters(['rolesCode'])
  },
  created() {
    this.productId = this.$route.query.productId
    this.queryReport()
  },
  methods: {
    // 查询测试结果
    queryReport() {
      this.loading = true
      getReport(this.productId).then(res => {
        this.testResultData = res.data.data
      }).catch(err => {
        this.$message.error(err.message)
      }).finally(() => {
        this.loading = false
      })
    },
    // 下载文件名称
    fileName(showName, productId) {
      const extension = showName.slice(showName.lastIndexOf('.')) // Extract the file extension
      const nameWithoutExtension = showName.slice(0, showName.lastIndexOf('.')) // Get the file name without extension
      return `${nameWithoutExtension}-${productId}${extension}` // Concatenate name, productId, and extension
    },
    // 下载报告
    downloadReport() {
      this.downBlobFile(this.testResultData.reportUrl, {}, this.fileName(this.testResultData.showName, this.testResultData.productId))
    },
    // 上传测试报告
    uploadReport() {
      this.dialogVisible = true
    },
    // 上传之前做校验
    beforeUpload(file) {
      // 在上传之前检查文件类型
      const extension = file.name.split('.').pop() // 获取上传文件的扩展名
      const allowedExtensions = ['pdf', 'xls', 'xlsx', 'doc', 'docx']

      if (!allowedExtensions.includes(extension)) {
        this.$message.error('只允许上传 pdf、xls、xlsx、doc、docx')
        return false // 阻止文件上传
      }

      return true // 允许文件上传
    },
    // 文件上传成功
    uploadSuccess(response, file, fileList) {
      this.file = file
      this.uploadSuccessedFile = true
      this.reportUrl = response.data.url
      this.showName = response.data.oldFilename
    },
    // 文件上传失败
    uploadError(err, file, fileList) {
      this.$message.error(err)
    },
    // 保存
    onsubmit() {
      const params = {
        productId: this.productId,
        testResult: this.form.testResult,
        reportUrl: this.reportUrl,
        showName: this.showName
      }
      addReport(params).then(res => {
        this.queryReport()
        this.dialogVisible = false
        this.form.testResult = ''
        this.reportUrl = ''
        this.uploadSuccessedFile = false
      }).catch(err => {
        this.$message.error(err.message)
      })
    },
    // 取消
    handleClose() {
      this.dialogVisible = false
      this.form.testResult = ''
      this.reportUrl = ''
      this.uploadSuccessedFile = false
    }
  }
}
</script>
<style lang="scss" scoped>
.product-test {
  font-size: 14px;

  .title {
    font-weight: 600;
    color: #252525;
  }

  .result {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-top: 24px;
    margin-bottom: 28px;
    border: 1px solid #ECEEF0;
    border-radius: 4px 4px 4px 4px;
    height: 156px;

    .box {
      line-height: 28px;

      .label {
        display: inline-block;
        width: 124px;
        font-weight: 400;
        color: #808080;
      }

      .result-data {
        .icon-shangchuanchenggong {
          // color: #3fc47a;
        }
      }
    }
  }

  .no-data {
    text-align: center;
    font-weight: 400;
    color: #7A7B80;

    img {
      width: 70px;
      height: 57px;
    }

    .no-text {
      margin-top: 15px;
    }
  }

  .btn {
    text-align: center;

    .download {
      margin-right: 19px;
    }

    .box {
      display: flex;
      align-items: center;

      .text {
        margin-left: 2px;
      }
    }
  }

  .list {
    padding: 20px 95px;

    .form {
      font-weight: 400;
      color: #6D7075;

      .upload-btn,
      .file-name {
        width: 357px;
        margin-top: 16px;
        color: #275FE8;
        font-size: 14px;
        height: 32px;

        ::v-deep .el-input__inner {
          color: #275FE8;
        }
      }

      .el-upload__tip {
        font-size: 14px;
        color: #808080;
      }

      ::v-deep .el-upload-list {
        display: none;
      }
    }
  }

  .close {
    font-weight: 400;
    color: #252525;
  }
}
</style>
